<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>현재 장애</title>
	<jsp:include page="/WEB-INF/views/common/header.jsp" />
	<script type="text/javascript" src="<c:out value="${pageContext.request.contextPath}" />/resources/common/js/bootstrap/js/bootstrap.min.js"></script>
	<script type="text/javascript" src="<c:out value="${pageContext.request.contextPath}" />/resources/common/js/knockout-3.0.0.js"></script>
	<script type="text/javascript" src="<c:out value="${pageContext.request.contextPath}" />/resources/common/js/sockjs-0.3.4.min.js" ></script>
	<script type="text/javascript" src="<c:out value="${pageContext.request.contextPath}" />/resources/common/js/stomp.js" ></script>

	<%-- URL for the STOMP end point for registering a stomp client--%>
	<c:url value="/alarmmessages" var="socketDest" />

	<script type="text/javascript">
		/***********************************************/
		/* PLEASE READ UP ON STOMP AND SOCKJS          */
		/* 1) http://jmesnil.net/stomp-websocket/doc/  */
		/* 2) https://github.com/sockjs/sockjs-client  */
		/***********************************************/
	
		//Declare a stompclient which will connect to the server
		var stompClient = null;
		
		/************************************************************************** 
		/*  JQUERY WAY OF BEING UNOBTRUSIVE AND ADDING EVENT HANDLERS TO WIDGETS, 
		/*  THUS KEEPING HTML AND JAVASCRIPT SEPARATE 
		/*************************************************************************/
		// Runs this code only when the DOM (all elements) are ready
		$(document).ready(function() {
			connect();
		});
		
		function connect() {
			console.log("Connecting..");
			
			// Register a websocket endpoint using SockJS and stomp.js
	        // Refer to Java class Refer to Java class 
	        // WebSocketConfig.java#registerStompEndpoints(StompEndpointRegistry registry)
	        var socket = new SockJS('${socketDest}');
			
	        console.log("SockJS init..");
	        
	        stompClient = Stomp.over(socket);
	        
	        console.log("stompClient init..");
	        
			// Now that a stomp client is defined, its time to open a connection
	        // 1) First we connect to the websocket server
	        // Notice that we dont pass in username and password as Spring Security
	        // has already provided the server with the Principal object containing user credentials
	        // 2) The last argument is a callback function which is called when connection succeeds
	        stompClient.connect('', '', function(frame) {
	            // In production code remove the next line
	            console.log("Connected: " + frame);
	
	            // Now subscribe to a topic of interest.
	            // Refer to Java class WebsocketBroadcastController.java#processMessageFromClient(SimpleMessage message)
	            // WebsocketBroadcastController is waiting for connections. Upon successful connection, it subscribes to
	            // the "/topic/simplemessagesresponse" destination where the server will echo the messages.
	            // When a broadcast message is received by the client on that destination, it will be shown by appending
	            // a paragraph to the DOM in the client browser.
	            stompClient.subscribe("/topic/alarmmessagesresponse", function(servermessage) {//Callback when server responds
	            	//alert("receive");
	                //alert(JSON.parse(servermessage.body).messageContent);
	                var msg = JSON.parse(servermessage.body);

	                alert("sev:" + msg.sev + ", alarmId:" + msg.alarmId + ", loc:" + msg.loc + 
	                		", alarmMsg:" + msg.alarmMsg + ", eventTime:" + msg.eventTime);

	                // 테이블에 row 추가
	            });
	        });        
		}

		function sendAlarm() {
			/*
            var alarmMsg = new Object();
            
            alarmMsg.sev 		= "CRITICAL";
            alarmMsg.alarmId 	= "A1000";
            alarmMsg.loc		= "Seoul";
            alarmMsg.alarmMsg	= "NETWORK DOWN";
            alarmMsg.eventTime	= "2014-03-17 00:00:00";
            alarmMsg.ackUser	= "";
            alarmMsg.ackTime	= "";
            */
            var alarmMsg = { sev:'CRITICAL', alarmId:'A1000', loc:'Seoul', alarmMsg:'NETWORK DOWN', 
            		eventTime:'2014-03-17 00:00:00', ackUser:'', ackTime:''};
            
            var jsonText = JSON.stringify(alarmMsg);
            
            sendMessageToServer(jsonText);
		}
		
		// Function to send the message typed in by the user to the "/app/simplemessages" destination on the server.
	    // WebsocketBroadcastController will receive this message and broadcast the results after 
	    // an artificially introduced delay.
	    function sendMessageToServer(messageForServer) {
	        // The message for the server must be in JSON format. Also refer SimpleMessage.java POJO.
	        /*
	        stompClient.send("/app/simplemessages", {}, JSON.stringify({
	            'message' : "test"
	        }));
	        */

	    	stompClient.send("/app/alarmmessages", {}, messageForServer);
	    }
	</script>
</head>
<body>
	<table>
		<tr>
			<th>등급</th>
			<th>알람ID</th>
			<th>위치</th>
			<th>메시지</th>
			<th>발생시간</th>
			<th>확인자</th>
			<th>확인시간</th>
		</tr>
	</table>
</body>
</html>